package com.newhopegroup.webcommons.utils;

import org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer;

import javax.sql.DataSource;

/**
 * Created by wangshida on 15/7/1.
 * 生成全局唯一ID组件
 * usage:
 *
 * <bean id="uuid" class="com.newhopegroup.webcommons.utils.UUID">
 <constructor-arg name="dataSource" ref="dataSource"/>
 <constructor-arg name="columnName" value="id"/>
 <constructor-arg name="incrementerName" value="uuid_sequence"/>
 <property name="cacheSize" value="50"/>
 </bean>
 *
 */
public class UUID extends MySQLMaxValueIncrementer {

    public UUID() {
    }

    public UUID(DataSource dataSource, String incrementerName, String columnName) {
        super(dataSource, incrementerName, columnName);
    }

    public final String generate() {
        Long seqR = getNextKey();
        return String.format("%019d%011d", System.currentTimeMillis(), seqR);
    }
}
